<template>
  <div class="">
    <el-table :data="$store.state.list" border style="width: 100%">
      <el-table-column prop="title" label="标题" width="180"> </el-table-column>
      <el-table-column prop="user" label="作者" width="180"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="remove(scope.$index)">删除</el-button>
          <el-button @click="edit(scope.row, scope.$index)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改" :visible.sync="show">
      <el-form :model="form">
        <el-form-item label="标题">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="form.user" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        title: "",
        user: "",
      },
      show: false,
      editIndex: -1,
      //要更新的数据的下标
    };
  },
  created() {},
  methods: {
    remove(index) {
      this.$store.commit("remove", index);
    },
    //删除
    edit(item, index) {
      this.show = true;
      this.form.title = item.title;
      this.form.user = item.user;
      this.editIndex = index;
    },
    //点击编辑回填并且保存要编辑的下标
    update() {
      this.$store.commit("update", {
        index: this.editIndex,
        title: this.form.title,
        user: this.form.user,
      });
      this.show = false;
    },
    //点击确定更新 传进vuex中对响应的下标的数据进行修改
  },
  components: {},
};
</script>

<style lang="scss"></style>
